###############################################################################
#                     Replication files for                                   #
#         "How do Americans Want Elections to be Run During                   #
#                       the COVID-19 Crisis? "                                #
#                                                                             #
#           Mackenzie Lockhart, Seth Hill, Jennifer Merolla,                  #
#                     Mindy Romero, Thad Kousser                              #
#                                                                             #
#                                                                             #
#                       Prepared by Mac Lockhart                              #
#                           Sep 11, 2020                                      #
###############################################################################



rm(list=ls())
library(tidyverse)
library(DeclareDesign)
library(survey)
library(srvyr)
library(ggpubr)

#load cleaned data
df<-read_csv("vbm_data_clean.csv")

##plotting policy preferences
strat_design <- df %>%
  as_survey_design(weight  = the.wts)

out <- strat_design %>%
  group_by(FL_16_DO) %>%
  summarize(vote_by_mail_mean = survey_mean(vote_by_mail, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            auto_by_mail_mean = survey_mean(auto_by_mail, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            only_by_mail_mean = survey_mean(only_by_mail, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            fed_funding_mean = survey_mean(fed_funding, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            n = unweighted(n()))
out2 <- strat_design %>%
  summarize(vote_by_mail_mean = survey_mean(vote_by_mail, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            auto_by_mail_mean = survey_mean(auto_by_mail, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            only_by_mail_mean = survey_mean(only_by_mail, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            fed_funding_mean = survey_mean(fed_funding, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            n = unweighted(n()))
out2$FL_16_DO <- "Overall"

out <- bind_rows(out, out2)
out$FL_16_DO <- c("Control", "Spring Peak Treatment", "Fall Peak Treatment", "Overall")
out$FL_16_DO <- factor(out$FL_16_DO,levels = c("Control", "Spring Peak Treatment", "Fall Peak Treatment", "Overall"))
out$type <- c("Condition", "Condition", "Condition", "Full")

for(i in 2:13){
  out[i] <- out[i]*100
}

g1<-ggplot(data = out, aes(x = FL_16_DO, fill=type,
                           ymax = vote_by_mail_mean_upp, ymin = vote_by_mail_mean_low)) + ylim(0, 100)+
  coord_cartesian(ylim=c(min(out$vote_by_mail_mean_low)-10, max(out$vote_by_mail_mean_upp)+10)) +
  geom_bar(stat = "identity", aes(y=vote_by_mail_mean), orientation="x", width=0.5, color="black") +
  geom_errorbar(position = position_dodge(width = 0.9), width = 0.1) +
  geom_text(aes(y = min(vote_by_mail_mean_low)-10, label = paste0(round(vote_by_mail_mean,1), "%")), position = position_dodge(width = 0.9), vjust = -1) +
  labs(title = "For the upcoming November 3 election, would you support national legislation directing \n all states to send a vote by mail ballot to any voter who requests one?", 
       color="Condition", x=element_blank(), y="Mean Support (%)") +
  scale_fill_grey(start = 0.5, end = 1) + theme(legend.position = "none")

g2<-ggplot(data = out, aes(x = FL_16_DO, fill=type,
                           ymax = auto_by_mail_mean_upp, ymin = auto_by_mail_mean_low)) + ylim(0, 100)+
  coord_cartesian(ylim=c(min(out$auto_by_mail_mean_low)-10, max(out$auto_by_mail_mean_upp)+10)) +
  geom_bar(stat = "identity", aes(y=auto_by_mail_mean), orientation="x", width=0.5, color="black") +
  geom_errorbar(position = position_dodge(width = 0.9), width = 0.1) +
  geom_text(aes(y = min(auto_by_mail_mean_low) -10, label = paste0(round(auto_by_mail_mean,1), "%")), position = position_dodge(width = 0.9), vjust = -1) +
  labs(title = "For the upcoming November 3 election, would you support national legislation directing \n all states to send a vote by mail ballot to every voter, even if they do not request one in advance", 
       color="Condition", x=element_blank(), y="Mean Support (%)") +
  scale_fill_grey(start = 0.5, end = 1) + theme(legend.position = "none")

g3<-ggplot(data = out, aes(x = FL_16_DO, fill=type, 
                           ymax = only_by_mail_mean_upp, ymin = only_by_mail_mean_low)) + ylim(0, 100)+
  coord_cartesian(ylim=c(min(out$only_by_mail_mean_low)-10, max(out$only_by_mail_mean_upp)+10)) +
  geom_bar(stat = "identity", aes(y=only_by_mail_mean), orientation="x", width=0.5, color="black") +
  geom_errorbar(position = position_dodge(width = 0.9), width = 0.1) +
  geom_text(aes(y = min(only_by_mail_mean_low)-10, label = paste0(round(only_by_mail_mean,1), "%")), position = position_dodge(width = 0.9), vjust = -1) +
  labs(title = "For the upcoming November 3 election, would you support national legislation directing \n all states to shift entirely to voting by mail, without having polling places or other in-person voting?", 
       color="Condition", x=element_blank(), y="Mean Support (%)") +
  scale_fill_grey(start = 0.5, end = 1) + theme(legend.position = "none")

g4<-ggplot(data = out, aes(x = FL_16_DO, fill = type,
                           ymax = fed_funding_mean_upp, ymin = fed_funding_mean_low)) + ylim(0, 100)+
  coord_cartesian(ylim=c(min(out$fed_funding_mean_low)-10, max(out$fed_funding_mean_upp)+10)) +
  geom_bar(stat = "identity", aes(y=fed_funding_mean), orientation="x", width=0.5, color="black") +
  geom_errorbar(position = position_dodge(width = 0.9), width = 0.1) +
  geom_text(aes(y = fed_funding_mean_low, label = paste0(round(fed_funding_mean,1), "%")), position = position_dodge(width = 0.9), vjust = -1) +
  labs(title = "For the November 3rd election, should the federal government provide increased funding \n to state and local governments to ensure the availability of vote by mail ballots to all voters, \n to maintain in-person voting options, and to bolster online voter registration?  ", 
       color="Condition", x=element_blank(), y="Mean Support for Increased Funding (%)") +
  scale_fill_grey(start = 0.5, end = 1) + theme(legend.position = "none")

national_legislation_plots <- ggarrange(g1, g2, g3, g4, nrow=4, ncol=1, heights=2, widths=2)
ggsave("National_Legislation.pdf", national_legislation_plots, height=20, width=12, units="in")

##Personal preferences
out <- strat_design %>%
  group_by(FL_16_DO) %>%
  summarize(wait_in_line_mean = survey_mean(wait_in_line, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            volunteer_mean = survey_mean(volunteer, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            wait_in_line_distance_mean = survey_mean(wait_in_line_distance, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            volunteer_distance_mean = survey_mean(volunteer_distance, vartype = "ci", na.rm=T, level=(1-(0.05/15))),  
            drive_through_mean = survey_mean(drive_through, vartype = "ci", na.rm=T, level=(1-(0.05/15))),  
            n = unweighted(n()))
out2 <- strat_design %>%
  summarize(wait_in_line_mean = survey_mean(wait_in_line, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            volunteer_mean = survey_mean(volunteer, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            wait_in_line_distance_mean = survey_mean(wait_in_line_distance, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            volunteer_distance_mean = survey_mean(volunteer_distance, vartype = "ci", na.rm=T, level=(1-(0.05/15))),  
            drive_through_mean = survey_mean(drive_through, vartype = "ci", na.rm=T, level=(1-(0.05/15))),  
            n = unweighted(n()))
out2$FL_16_DO <- "Overall"

out <- bind_rows(out, out2)
out$FL_16_DO <- c("Control", "Spring Peak Treatment", "Fall Peak Treatment", "Overall")
out$FL_16_DO <- factor(out$FL_16_DO,levels = c("Control", "Spring Peak Treatment", "Fall Peak Treatment", "Overall"))
out$type <- c("Condition", "Condition", "Condition", "Full")

for(i in 2:16){
  out[i] <- out[i]*100
}

g1<-ggplot(data = out, aes(x = FL_16_DO,  fill = type,
                           ymax = wait_in_line_mean_upp, ymin = wait_in_line_mean_low)) + ylim(0, 100)+
  coord_cartesian(ylim=c(min(out$wait_in_line_mean_low)-10, max(out$wait_in_line_mean_upp)+10)) +
  geom_bar(stat = "identity", aes(y=wait_in_line_mean), orientation="x", width=0.5, color="black") +
  geom_errorbar(position = position_dodge(width = 0.9), width = 0.1) +
  geom_text(aes(y = min(wait_in_line_mean_low)-10, label = paste0(round(wait_in_line_mean,1), "%")), position = position_dodge(width = 0.9), vjust = -1) +
  labs(title = "In the upcoming November 3 election, would you be comfortable waiting in line at a polling place, \n if there were no social distancing measures implemented at that polling place?", 
       color="Condition", x=element_blank(), y="Mean Comfort (%)") +
  scale_fill_grey(start = 0.5, end = 1) + theme(legend.position = "none") 

g2<-ggplot(data = out, aes(x = FL_16_DO,  fill = type,
                           ymax = volunteer_mean_upp, ymin = volunteer_mean_low)) + ylim(0, 100)+
  coord_cartesian(ylim=c(min(out$volunteer_mean_low)-10, max(out$volunteer_mean_upp)+10)) +
  geom_bar(stat = "identity", aes(y=volunteer_mean), orientation="x", width=0.5, color="black") +
  geom_errorbar(position = position_dodge(width = 0.9), width = 0.1) +
  geom_text(aes(y = min(volunteer_mean_low)-10, label = paste0(round(volunteer_mean,1), "%")), position = position_dodge(width = 0.9), vjust = -1) +
  labs(title = "In the upcoming November 3 election, would you be willing to work as a poll worker, \n if there were no social distancing measures implemented at that polling place?", 
       color="Condition", x=element_blank(), y="Mean Willingness (%)") +
  scale_fill_grey(start = 0.5, end = 1) + theme(legend.position = "none") 

g3<-ggplot(data = out, aes(x = FL_16_DO,  fill = type,
                           ymax = wait_in_line_distance_mean_upp, ymin = wait_in_line_distance_mean_low)) + ylim(0, 100)+
  coord_cartesian(ylim=c(min(out$wait_in_line_distance_mean_low)-10, max(out$wait_in_line_distance_mean_upp)+10)) +
  geom_bar(stat = "identity", aes(y=wait_in_line_distance_mean), orientation="x", width=0.5, color="black") +
  geom_errorbar(position = position_dodge(width = 0.9), width = 0.1) +
  geom_text(aes(y = min(wait_in_line_distance_mean_low)-10, label = paste0(round(wait_in_line_distance_mean,1), "%")), position = position_dodge(width = 0.9), vjust = -1) +
  labs(title = "In the upcoming November 3 election, would you be comfortable waiting in line at a polling place, \n if the polling place was reconfigured in order to adhere to social distancing protocol, \n creating additional space between voting booths, poll workers, and voters standing in line, \n and to take other measures to protect the public's health?", 
       color="Condition", x=element_blank(), y="Mean Comfort (%)") +
  scale_fill_grey(start = 0.5, end = 1) + theme(legend.position = "none") 

g4<-ggplot(data = out, aes(x = FL_16_DO,  fill = type,
                           ymax = volunteer_distance_mean_upp, ymin = volunteer_distance_mean_low)) + ylim(0, 100)+
  coord_cartesian(ylim=c(min(out$volunteer_distance_mean_low)-10, max(out$volunteer_distance_mean_upp)+10)) +
  geom_bar(stat = "identity", aes(y=volunteer_distance_mean), orientation="x", width=0.5, color="black") +
  geom_errorbar(position = position_dodge(width = 0.9), width = 0.1) +
  geom_text(aes(y = min(volunteer_distance_mean_low)-10, label = paste0(round(volunteer_distance_mean,1), "%")), position = position_dodge(width = 0.9), vjust = -1) +
  labs(title = "In the upcoming November 3 election, would you be willing to work as a poll worker, \n if the polling place was reconfigured in order to adhere to social distancing protocol, \n creating additional space between voting booths, poll workers, and voters standing in line, \n and to take other measures to protect the public's health?", 
       color="Condition", x=element_blank(), y="Mean Willingness (%)") +
  scale_fill_grey(start = 0.5, end = 1) + theme(legend.position = "none") 

g5<-ggplot(data = out, aes(x = FL_16_DO,  fill = type,
                           ymax = drive_through_mean_upp, ymin = drive_through_mean_low)) + ylim(0, 100)+
  coord_cartesian(ylim=c(min(out$drive_through_mean_low)-10, max(out$drive_through_mean_upp)+10)) +
  geom_bar(stat = "identity", aes(y=drive_through_mean), orientation="x", width=0.5, color="black") +
  geom_errorbar(position = position_dodge(width = 0.9), width = 0.1) +
  geom_text(aes(y = min(drive_through_mean_low)-10, label = paste0(round(drive_through_mean,1), "%")), position = position_dodge(width = 0.9), vjust = -1) +
  labs(title = "In the upcoming November 3 election, would you be comfortable dropping off your ballot at a drive-through location \n professionally staffed by your county elections office, if it was reconfigured in order to adhere to social distancing protocol?", 
       color="Condition", x=element_blank(), y="Mean Comfort (%)") +
  scale_fill_grey(start = 0.5, end = 1) + theme(legend.position = "none") 

personal_comfort_plots <- ggarrange(g1, g3, g2, g4, g5, nrow=5, ncol=1, heights=2, widths=2)
ggsave("Personal_Comfort_Plots.pdf", personal_comfort_plots, width=12, height=20, units="in")


##Preferences
strat_design <- df %>%
  as_survey_design(weight  = the.wts)

out <- strat_design %>%
  group_by(FL_16_DO) %>%
  summarize(preffered_vote_mean = survey_mean(preffered_vote, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            desire_info_mean = survey_mean(desire_info, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            will_vote_polls_mean = survey_mean(will_vote_polls, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            might_vote_polls_mean = survey_mean(might_vote_polls, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            wont_vote_polls_mean = survey_mean(wont_vote_polls, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            will_vote_mail_mean = survey_mean(will_vote_mail, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            might_vote_mail_mean = survey_mean(might_vote_mail, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            wont_vote_mail_mean = survey_mean(wont_vote_mail, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            n = unweighted(n()))
out2 <- strat_design %>%
  summarize(preffered_vote_mean = survey_mean(preffered_vote, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            desire_info_mean = survey_mean(desire_info, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            will_vote_polls_mean = survey_mean(will_vote_polls, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            might_vote_polls_mean = survey_mean(might_vote_polls, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            wont_vote_polls_mean = survey_mean(wont_vote_polls, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            will_vote_mail_mean = survey_mean(will_vote_mail, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            might_vote_mail_mean = survey_mean(might_vote_mail, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            wont_vote_mail_mean = survey_mean(wont_vote_mail, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            n = unweighted(n()))

out2$FL_16_DO <- "Overall"

out <- bind_rows(out, out2)
out$FL_16_DO <- c("Control", "Spring Peak Treatment", "Fall Peak Treatment", "Overall")
out$FL_16_DO <- factor(out$FL_16_DO,levels = c("Control", "Spring Peak Treatment", "Fall Peak Treatment", "Overall"))
out$type <- c("Condition", "Condition", "Condition", "Full")

for(i in 2:25){
  out[i] <- out[i]*100
}


g1<-ggplot(data = out, aes(x = FL_16_DO,  fill = type,
                           ymax = preffered_vote_mean_upp, ymin = preffered_vote_mean_low)) + ylim(0, 100)+
  coord_cartesian(ylim=c(min(out$preffered_vote_mean_low)-10, max(out$preffered_vote_mean_upp)+10)) +
  geom_bar(stat = "identity", aes(y=preffered_vote_mean), orientation="x", width=0.5, color="black") +
  geom_errorbar(position = position_dodge(width = 0.9), width = 0.1) +
  geom_text(aes(y = min(preffered_vote_mean_low)-10, label = paste0(round(preffered_vote_mean,1), "%")), position = position_dodge(width = 0.9), vjust = -1) +
  labs(title = "In the upcoming November 3 election, if you had the ability to cast a ballot in any way you wished, \n what would be your most preferred way to cast a ballot?", 
       color="Condition", x=element_blank(), y="Mean Saying Vote-By-Mail (%)") +
  scale_fill_grey(start = 0.5, end = 1) + theme(legend.position = "none") 

g2<-ggplot(data = out, aes(x = FL_16_DO,  fill = type,
                           ymax = desire_info_mean_upp, ymin = desire_info_mean_low)) + ylim(0, 100)+
  coord_cartesian(ylim=c(min(out$desire_info_mean_low)-10, max(out$desire_info_mean_upp)+10)) +
  geom_bar(stat = "identity", aes(y=desire_info_mean), orientation="x", width=0.5, color="black") +
  geom_errorbar(position = position_dodge(width = 0.9), width = 0.1) +
  geom_text(aes(y = min(desire_info_mean_low)-10, label = paste0(round(desire_info_mean,1), "%")), position = position_dodge(width = 0.9), vjust = -1) +
  labs(title = "If you would like to learn how to apply for a vote by mail ballot in your state, if that is allowed, \n you can visit a guide provided by the federal government at the link below.", 
       color="Condition", x=element_blank(), y="Mean Requesting More Information (%)") +
  scale_fill_grey(start = 0.5, end = 1) + theme(legend.position = "none") 


##Subset to those who say they will vote in November
df1 <- subset(df, df$vote_intent==1)
strat_design <- df1 %>%
  as_survey_design(weight  = the.wts)

out <- strat_design %>%
  group_by(FL_16_DO) %>%
  summarize(preffered_vote_mean = survey_mean(preffered_vote, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            desire_info_mean = survey_mean(desire_info, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            will_vote_polls_mean = survey_mean(will_vote_polls, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            might_vote_polls_mean = survey_mean(might_vote_polls, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            wont_vote_polls_mean = survey_mean(wont_vote_polls, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            will_vote_mail_mean = survey_mean(will_vote_mail, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            might_vote_mail_mean = survey_mean(might_vote_mail, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            wont_vote_mail_mean = survey_mean(wont_vote_mail, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            n = unweighted(n()))
out2 <- strat_design %>%
  summarize(preffered_vote_mean = survey_mean(preffered_vote, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            desire_info_mean = survey_mean(desire_info, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            will_vote_polls_mean = survey_mean(will_vote_polls, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            might_vote_polls_mean = survey_mean(might_vote_polls, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            wont_vote_polls_mean = survey_mean(wont_vote_polls, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            will_vote_mail_mean = survey_mean(will_vote_mail, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            might_vote_mail_mean = survey_mean(might_vote_mail, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            wont_vote_mail_mean = survey_mean(wont_vote_mail, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            n = unweighted(n()))

out2$FL_16_DO <- "Overall"

out <- bind_rows(out, out2)
out$FL_16_DO <- c("Control", "Spring Peak Treatment", "Fall Peak Treatment", "Overall")
out$FL_16_DO <- factor(out$FL_16_DO,levels = c("Control", "Spring Peak Treatment", "Fall Peak Treatment", "Overall"))
out$type <- c("Condition", "Condition", "Condition", "Full")

for(i in 2:25){
  out[i] <- out[i]*100
}

vote_in_person <- c(rep("Will vote", 4), rep("Might vote", 4), rep("Wont vote", 4))
vote_in_person_mean <-as.numeric(c(out$will_vote_polls_mean, out$might_vote_polls_mean, out$wont_vote_polls_mean))
vote_in_person_low <-c(out$will_vote_polls_mean_low, out$might_vote_polls_mean_low, out$wont_vote_polls_mean_low)
vote_in_person_upp <-c(out$will_vote_polls_mean_upp, out$might_vote_polls_mean_upp, out$wont_vote_polls_mean_upp)
treatment <- factor(rep(c("Control", "Spring Peak Treatment", "Fall Peak Treatment", "Overall"), 3), 
                    levels = c("Control", "Spring Peak Treatment", "Fall Peak Treatment", "Overall"))
type <- c(rep(out$type <- c("Condition", "Condition", "Condition", "Full"), 3))
temp1 <- data.frame(vote_in_person, vote_in_person_mean, vote_in_person_low, vote_in_person_upp, treatment, type)


g3<- ggplot(temp1, aes(fill= treatment,y=vote_in_person_mean, x=vote_in_person,
                       ymax = vote_in_person_upp, ymin = vote_in_person_low)) + ylim(0, 100) +
  geom_bar(position="dodge", stat="identity", orientation="x",color="black") +
  geom_errorbar(position = position_dodge(width = 0.9), width = 0.1) +
  scale_fill_grey(start = 0.5, end = 1) +
  geom_text(aes(y = 0, label = paste0(round(vote_in_person_mean,1), "%")), position = position_dodge(width = 0.8), vjust = -0.3, size=3) +
  labs(title = "Q In the upcoming November 3 election, if your only option to cast a ballot in your county was an in-person option such as \n a traditional polling place or a professionally staffed county elections office, how likely would you be to cast a ballot?", 
       color="Condition", x=element_blank(), y="Percent Giving Response (%)", fill="Condition")


vote_by_mail <- c(rep("Will vote", 4), rep("Might vote", 4), rep("Wont vote", 4))
vote_by_mail_mean <-as.numeric(c(out$will_vote_mail_mean, out$might_vote_mail_mean, out$wont_vote_mail_mean))
vote_by_mail_low <-c(out$will_vote_mail_mean_low, out$might_vote_mail_mean_low, out$wont_vote_mail_mean_low)
vote_by_mail_upp <-c(out$will_vote_mail_mean_upp, out$might_vote_mail_mean_upp, out$wont_vote_mail_mean_upp)
treatment <- factor(rep(c("Control", "Spring Peak Treatment", "Fall Peak Treatment", "Overall"), 3), 
                    levels = c("Control", "Spring Peak Treatment", "Fall Peak Treatment", "Overall"))
type <- c(rep(out$type <- c("Condition", "Condition", "Condition", "Full"), 3))
temp2 <- data.frame(vote_by_mail, vote_by_mail_mean, vote_by_mail_low, vote_by_mail_upp, treatment, type)


g4<- ggplot(temp2, aes(fill= treatment,y=vote_by_mail_mean, x=vote_by_mail,
                       ymax = vote_by_mail_upp, ymin = vote_by_mail_low)) + ylim(0, 100) +
  geom_bar(position="dodge", stat="identity", orientation="x",color="black") +
  geom_errorbar(position = position_dodge(width = 0.9), width = 0.1) +
  scale_fill_grey(start = 0.5, end = 1) +
  geom_text(aes(y = 0, label = paste0(round(vote_by_mail_mean,1), "%")), position = position_dodge(width = 0.9), vjust = -0.3, size=3) +
  labs(title = "In the upcoming November 3 election, if your only option to cast a ballot in your county was to \n vote by mailing in a ballot, how likely would you be to cast a ballot?", 
       color="Condition", x=element_blank(), y="Percent Giving Response (%)", fill="Condition")


vote_preferences_plots <- ggarrange(g1, g2, g3, g4, nrow=4, ncol=1, heights=2, widths=2)
ggsave("Voting_Preferences.pdf", vote_preferences_plots, height=20, width=12.5, units="in")

##Confidence and integrity
strat_design <- df %>%
  as_survey_design(weight  = the.wts)

out <- strat_design %>%
  group_by(FL_16_DO) %>%
  summarize(confidence_vote_mean = survey_mean(confidence_vote, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            integrity_mean = survey_mean(integrity, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            n = unweighted(n()))
out2 <- strat_design %>%
  summarize(confidence_vote_mean = survey_mean(confidence_vote, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            integrity_mean = survey_mean(integrity, vartype = "ci", na.rm=T, level=(1-(0.05/15))),
            n = unweighted(n()))

out2$FL_16_DO <- "Overall"

out <- bind_rows(out, out2)
out$FL_16_DO <- c("Control", "Spring Peak Treatment", "Fall Peak Treatment", "Overall")
out$FL_16_DO <- factor(out$FL_16_DO,levels = c("Control", "Spring Peak Treatment", "Fall Peak Treatment", "Overall"))
out$type <- c("Condition", "Condition", "Condition", "Full")

for(i in 2:7){
  out[i] <- out[i]*100
}

g1<-ggplot(data = out, aes(x = FL_16_DO,  fill = type,
                           ymax = confidence_vote_mean_upp, ymin = confidence_vote_mean_low)) + ylim(0, 100)+
  coord_cartesian(ylim=c(min(out$confidence_vote_mean_low)-10, max(out$confidence_vote_mean_upp)+10)) +
  geom_bar(stat = "identity", aes(y=confidence_vote_mean), orientation="x", width=0.5, color="black") +
  geom_errorbar(position = position_dodge(width = 0.9), width = 0.1) +
  geom_text(aes(y = min(confidence_vote_mean_low)-10, label = paste0(round(confidence_vote_mean,1), "%")), position = position_dodge(width = 0.9), vjust = -1) +
  labs(title = "In the upcoming November 3 election, if you had the ability to cast a ballot in any way you wished, \n what would give you the most confidence that your own ballot would be counted correctly?", 
       color="Condition", x=element_blank(), y="Mean Saying Vote-By-Mail (%)") +
  scale_fill_grey(start = 0.5, end = 1) + theme(legend.position = "none")

g2<-ggplot(data = out, aes(x = FL_16_DO,  fill = type,
                           ymax = integrity_mean_upp, ymin = integrity_mean_low)) + ylim(0, 100)+
  coord_cartesian(ylim=c(min(out$integrity_mean_low)-10, max(out$integrity_mean_upp)+10)) +
  geom_bar(stat = "identity", aes(y=integrity_mean), orientation="x", width=0.5, color="black") +
  geom_errorbar(position = position_dodge(width = 0.9), width = 0.1) +
  geom_text(aes(y = min(integrity_mean_low)-10, label = paste0(round(integrity_mean,1), "%")), position = position_dodge(width = 0.9), vjust = -1) +
  labs(title = "In the upcoming November 3 election, if other voters have the ability to cast a ballot in any way they wish, \n what would give you the most confidence in the integrity of the election results?", 
       color="Condition", x=element_blank(), y="Mean Saying Vote-By-Mail (%)") +
  scale_fill_grey(start = 0.5, end = 1) + theme(legend.position = "none")

election_preferences_plots <- ggarrange(g1, g2, nrow=2, ncol=1, heights=2, widths=2)
ggsave("Election_Preferences.pdf", election_preferences_plots, height=10, width=12.5, units="in")

